﻿using Abp.Domain.Repositories;
using Abp.UI;
using shenghua.Common.WordNodes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Basics.ShippingTemplates
{
    /// <summary>
    /// 运费模板。所有模板统一处理，可以勾选是固定模板还是阶梯模板，可以选择配送区域，可以设置包邮条件
    /// </summary>
    public class ShippingTemplateManager : WordNodeManager<ShippingTemplate>
    {
        private readonly SettingManager _settingManager;

        public ShippingTemplateManager(IRepository<ShippingTemplate> shippingTemplateRepository, SettingManager settingManager)
            : base(shippingTemplateRepository)
        {
            _settingManager = settingManager;
        }

        public override async Task<ShippingTemplate> Add(ShippingTemplate shippingTemplate)
        {
            return await base.Add(shippingTemplate);
        }

        public override async Task<ShippingTemplate> Update(ShippingTemplate shippingTemplate)
        {
            return await base.Update(shippingTemplate);
        }

        /// <summary>
        /// 设置模板类型
        /// </summary>
        /// <param name="shippingTemplateType"></param>
        /// <returns></returns>
        public async Task SetTemplateType(int id, ShippingTemplateType shippingTemplateType)
        {
            var item = await Get(id);
            item.TemplateType = shippingTemplateType;
            await Update(item);
        }

        /// <summary>
        /// 设置配送区域
        /// </summary>
        /// <param name="areas"></param>
        /// <returns></returns>
        public async Task SetAreas(int id, List<string> areas)
        {
            var item = await Get(id);
            item.Areas = new Common.CustomValueType.ValueArray<string>(areas);
            await Update(item);
        }

        /// <summary>
        /// 设置是否允许包邮
        /// </summary>
        /// <param name="allowFreeShipping"></param>
        /// <returns></returns>
        public async Task SetFreeShipping(int id, bool allowFreeShipping)
        {
            var item = await Get(id);
            item.AllowFreeShipping = allowFreeShipping;
            await Update(item);
        }

        /// <summary>
        /// 设置包邮条件
        /// </summary>
        /// <param name="freeShippingConditions"></param>
        /// <returns></returns>
        public async Task SetFreeShippingConditions(int id, List<FreeShippingCondition> freeShippingConditions)
        {
            var item = await Get(id);
            item.SetFreeShippingConditions(freeShippingConditions);
            await Update(item);
        }

        public async Task<ShippingTemplate> GetValid(int id)
        {
            var shippingTemplate = await Get(id);
            if (!shippingTemplate.Enabled)
            {
                throw new UserFriendlyException("运费模板未启用");
            }
            if (shippingTemplate.TemplateType != _settingManager.GetShippingTemplateType())
            {
                throw new UserFriendlyException("无效的运费模板类型");
            }
            return shippingTemplate;
        }

        public async Task<List<ShippingTemplate>> GetCanUsed(string areaTag)
        {
            var templateType = _settingManager.GetShippingTemplateType();
            return _repository.GetAll().Where(r => r.Enabled && r.TemplateType == templateType && r.Areas.StringValue.Contains(areaTag)).ToList();
        }
    }
}
